const Video_service = require("../sercice/VideoSevire")


const Video_Control = {
    add_video: async (req, res) => {

        try {
            const result = await Video_service.video_add(req.body)
            res.status(200).send({
                msg: "添加成功",
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "添加失败",
                code: 411
            })
        }
    },
    uplaod: async (req, res) => {
        try {
            console.log("??///")
            const filePath = req.file.path
            // 将路径\\转化为/ 'public\\images\\user\\1716630232816-664794344.png',
            const normalizedPath = filePath.replace(/\\/g, '/');
            //去掉路径中的public
            const relativePath = normalizedPath.split('public')[1];
            res.status(200).send({
                msg: "上传成功",
                code: 200,
                data: {
                    url: relativePath
                }
            })
        } catch (error) {
            res.status(411).send({
                msg: "上传失败",
                code: 411
            })
        }

    },
    get_list: async (req, res) => {
        try {
            const result = await Video_service.get_list(req.params.id)
            res.status(200).send({
                msg: "获取成功",
                data: result.data,
                count: result.count,
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "获取失败",
                data: [],
                count: 0
            })
        }
    },
    get_one: async (req, res) => {
        try {
            const result = await Video_service.get_one(req.params.id)
            res.status(200).send({
                msg: "获取成功",
                data: result,
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "获取失败",
                data: '',
                code: 411
            })
        }
    },
    put_one: async (req, res) => {
        try {
            const result = await Video_service.put_video(req.body)
            if (result.matchedCount === 1) {
                res.status(200).send({
                    msg: "修改成功",
                    code: 200,

                })
            }
        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                code: 411,

            })
        }
    },
    delete_one: async (req, res) => {
        try {
            const result = await Video_service.delete_video(req.params.id)
            res.status(200).send({
                msg: "删除成功",
                code: 200,

            })
        } catch (error) {
            res.status(411).send({
                msg: "删除失败",
                code: 411,

            })
        }
    }
}



module.exports = Video_Control